package edu.siu.math.egut.main;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;

import edu.siu.math.egut.io.Algorithms;
import edu.siu.math.egut.io.ParabolicException;
import edu.siu.math.egut.io.ParabolicPieceException;
import edu.siu.math.egut.util.Group;
import edu.siu.math.egut.util.NotARootException;
import edu.siu.math.egut.util.Parabolic;
import edu.siu.math.egut.util.ParabolicReadException;
import edu.siu.math.egut.util.Piece;
import edu.siu.math.egut.util.Polynomial;
import edu.siu.math.egut.util.PolynomialReadException;

public class LieAlgebraStabilizer {
	private static final String INSTRUCTIONS = "This program requires at least three arguments:\n"
	    + "(1) the name of an exceptional group\n"
	    + "(2) a proper parabolic subgroup, specified by naming the simple roots in the unipotent radical\n"
	    + "(3) a piece of that unipotent radical, specified either by\n"
	    + "\t(a) giving an integral weight for each root named at step (2), or\n"
	    + "\t(b) simply giving a single integer,\n" 
	    + "(4) a Polynomial in variables u_\\alpha as \\alpha ranges over roots in the piece."
	    + "EXAMPLE: F4 4 2\n" 
	    + "You can also invoke it with the name of a plaintext file which contains such input.";
   
    private class Input {
	private final Group g;
	private final Parabolic p;
	private final Piece r;
	private final Polynomial ell;

	public Group getG() {
	    return g;
	}
	public Parabolic getP() {
	    return p;
	}
	public Piece getR() {
	    return r;
	}
	public Polynomial getEll() {
	    return ell;
	}
	public Input(Group g, Parabolic p, Piece r, Polynomial ell) {
	    super();
	    this.g = g;
	    this.p = p;
	    this.r = r;
	    this.ell = ell;
	}
    }

    public static void main(String[] args) {
	
	new LieAlgebraStabilizer(args);
    }
    
    
    
    public LieAlgebraStabilizer(String[] args) {
	super();
	Input input = getInput(args);
	
	ArrayList<Polynomial> images = new ArrayList<Polynomial>();
	
	//Let each coroot act
	images.addAll(corootImages(input));
	
	// Let each positive root vector act
	images.addAll( positiveRootImages(input));
	
	// Let each negative root vector act
	images.addAll(negativeRootImages(input));
	
	// Set linear combination equal to zero, solve for relations on the coefficients
	
    }



    private ArrayList<Polynomial> negativeRootImages(Input input) {
	
	return null;
    }



    private ArrayList<Polynomial> positiveRootImages(Input input) {
	// TODO Auto-generated method stub
	return null;
    }



    private ArrayList<Polynomial> corootImages(Input input) {
	ArrayList<Polynomial> answer = new ArrayList<Polynomial>();
	for( int i = 0; i < input.getG().getRank(); i++)
	    answer.add(corootAction(i, input));
	return answer;
    }



    private Polynomial corootAction(int i, Input input) {
	
	return null;
    }



    public  Input getInput( String[] args){
	if( args.length == 0 ){
	    try {
		System.out.println(CharacterAction2.NO_ARGS_INSTRUCTIONS);
		args=Algorithms.getCommandsFromFile(
			Algorithms.getFileName(new Scanner(System.in)));
	    } catch (FileNotFoundException e) {
		e.printStackTrace();
	    }
	}
	if (args.length == 1) {
	    /* Interpret as filename.  Algorithms.getInput will 
	     * package the contents of that file as a String[].  Call 
	     * this method on that String[].
	     */
	    return getInput( Algorithms.getInput(args));
	}
	if( args.length == 4){
		final Group g = Group.getGroup(args[0]);
		if (g == null) {
		    System.out.println("First argument --" + args[0]
			    + "-- is not the name of an exceptional group.");
		    System.exit(1);
		}
		try {
		    /*
		     * args[1] should specify a parabolic subgroup of that exceptional
		     * group. therefore, (1) every character in it should be a digit
		     * between 1 and the rank of g inclusive, and (2) they should all be
		     * different. Those conditions being satisfied, the interpretation
		     * is that you are listing those simple roots such that the
		     * corresponding root subgroup is in the unipotent radical.
		     */

		    final Parabolic p= Parabolic.read(g,CharacterAction2.parseParabolic(args[1], g));

		    /*
		     * args[2] should specify a piece. Can do it in one of two ways (a)
		     * an integer for each root listed in args[1] (b) a single integer.
		     */

		    final Piece r = Piece.create(p, args[2]);
		    if( r== null){
			System.out.println("Third argument -- " + args[2] + " -- " +
					"could not be parsed to specify a piece of the " +
					"parabolic "+ args[1] + " in "+ args[0]+".\n");
		    }
		    final Polynomial ell = Polynomial.read(args[3]);
		    
		    return this.new Input(g, p, r, ell);
		    
		} catch (final ParabolicException e) {
		    
		    System.out
			    .println("Second argument -- "
				    + args[1]
				    + "-- could not be parsed as a parabolic in "
				    + args[0]
				    + ".\n"
				    + "required format is to list the simple roots that are in the unipotent radical,\n"
				    + "all mashed together.  Example: 134");
		    // TODO add flexibility on this format.
		    e.printStackTrace();

		} catch (ParabolicReadException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		} catch (PolynomialReadException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		return null;


	}
	

	/* If none of those if statements was triggered, then we have 
	 * input that tends to indicate user is not familiar with the 
	 * syntax for this program...
	 */
	    System.out.println(INSTRUCTIONS);
	    System.exit(1);
	    return null;
	

    }
    

}
